import { defineStore } from 'pinia'
import { getAccount, getSubcount } from '@/utils/api'
import { UserAccount } from '@/models/user'
import { checkQR } from '@/models/loginAndRegister'

export const useUserStore = defineStore("user", {
    state: () => {
        return {
            cookie: '',
            profile: {} as UserAccount
        }
    },
    actions: {
        /* 获得账号信息 */
        async getUserDetail() {
            // console.log('进来了')
            if (localStorage.getItem('USER')) {
                this.profile = JSON.parse(localStorage.getItem('USER') || '')
            } else {
                const data = await getAccount()
                // console.log(data)
                if (data.code == 200) {
                    // console.log('登录成功')
                    this.profile = data.profile
                    localStorage.setItem("USER", JSON.stringify(this.profile))
                }
            }
        },
        async getUserCookie(data: checkQR) {
            this.cookie = data.cookie
            localStorage.setItem("USER-COOKIE", this.cookie)
            // console.log('正在进入')
            await this.getUserDetail()
        },
    },
    getters: {

    }
})